import numpy as np
import torch

def calculate_EN(image):
    # 计算图像的信息熵
    histogram ,bins = np.histogram(image,bins=256,range=(0,255))
    histogram = histogram / float(np.sum(histogram))
    entropy = -np.sum(histogram *np.log2(histogram + 1e-7))
    return entropy

def calculate_SNR(image):
    # 计算图像的信噪比
    signal_power = torch.mean(image ** 2)
    noise_power = torch.mean((image-torch.mean(image)) ** 2)
    snr = 10 * torch.log10(signal_power/noise_power)
    return snr.item()

def calculate_SF(image):
    # 计算图像的空间频率
    image_array = np.array(image)
    RF = np.diff(image_array, axis = 0)
    RF1 = np.sqrt(np.mean(np.mean(RF ** 2)))
    CF = np.diff(image_array, axis=1)
    CF1 = np.sqrt(np.mean(np.mean(CF ** 2)))
    SF = np.sqrt(RF1 ** 2 + CF1 ** 2)
    return SF

def calculate_SD(image_array):
    # 计算图像的标准差
    m, n = image_array.shape
    u = np.mean(image_array)
    SD = np.sqrt(np.sum(np.sum((image_array - u) ** 2)) / (m * n))
    return SD

def calculate_AG(image):
    # 计算图像平均梯度
    width = image.shape[1]
    width = width - 1
    height = image.shape[0]
    height = height - 1
    tmp = 0.0
    [grady, gradx] = np.gradient(image)
    s = np.sqrt((np.square(gradx) + np.square(grady)) / 2)
    AG = np.sum(np.sum(s)) / (width * height)
    return AG